﻿
using System;
using System.Data;
using System.Text;
using System.Data.SQLite;
using PrjSerails.DAL;
using PrjSerails.Model;

namespace PrjSerails.DAL
{
	/// <summary>
	/// 数据访问类:TandHTable
	/// </summary>
	public partial class TandHTable
	{
		public TandHTable()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQLite.GetMaxID("Id", "TandHTable"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from TandHTable");
			strSql.Append(" where Id=@Id ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@Id", DbType.Int32,8)			};
			parameters[0].Value = Id;

			return DbHelperSQLite.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(PrjSerails.Model.TandHTable model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into TandHTable(");
			strSql.Append("HumValue,GetDateTime,TemValue,AreaId,Id)");
			strSql.Append(" values (");
			strSql.Append("@HumValue,@GetDateTime,@TemValue,@AreaId,@Id)");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@HumValue", DbType.Int32,8),
					new SQLiteParameter("@GetDateTime", DbType.DateTime),
					new SQLiteParameter("@TemValue", DbType.Decimal,4),
					new SQLiteParameter("@AreaId", DbType.Int32,8),
					new SQLiteParameter("@Id", DbType.Int32,8)};
			parameters[0].Value = model.HumValue;
			parameters[1].Value = model.GetDateTime;
			parameters[2].Value = model.TemValue;
			parameters[3].Value = model.AreaId;
			parameters[4].Value = model.Id;

			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(PrjSerails.Model.TandHTable model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update TandHTable set ");
			strSql.Append("HumValue=@HumValue,");
			strSql.Append("GetDateTime=@GetDateTime,");
			strSql.Append("TemValue=@TemValue,");
			strSql.Append("AreaId=@AreaId");
			strSql.Append(" where Id=@Id ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@HumValue", DbType.Int32,8),
					new SQLiteParameter("@GetDateTime", DbType.DateTime),
					new SQLiteParameter("@TemValue", DbType.Decimal,4),
					new SQLiteParameter("@AreaId", DbType.Int32,8),
					new SQLiteParameter("@Id", DbType.Int32,8)};
			parameters[0].Value = model.HumValue;
			parameters[1].Value = model.GetDateTime;
			parameters[2].Value = model.TemValue;
			parameters[3].Value = model.AreaId;
			parameters[4].Value = model.Id;

			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from TandHTable ");
			strSql.Append(" where Id=@Id ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@Id", DbType.Int32,8)			};
			parameters[0].Value = Id;

			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string Idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from TandHTable ");
			strSql.Append(" where Id in ("+Idlist + ")  ");
			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public PrjSerails.Model.TandHTable GetModel(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select HumValue,GetDateTime,TemValue,AreaId,Id from TandHTable ");
			strSql.Append(" where Id=@Id ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@Id", DbType.Int32,8)			};
			parameters[0].Value = Id;

			PrjSerails.Model.TandHTable model=new PrjSerails.Model.TandHTable();
			DataSet ds=DbHelperSQLite.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public PrjSerails.Model.TandHTable DataRowToModel(DataRow row)
		{
			PrjSerails.Model.TandHTable model=new PrjSerails.Model.TandHTable();
			if (row != null)
			{
				if(row["HumValue"]!=null && row["HumValue"].ToString()!="")
				{
					model.HumValue=int.Parse(row["HumValue"].ToString());
				}
				if(row["GetDateTime"]!=null && row["GetDateTime"].ToString()!="")
				{
					model.GetDateTime=DateTime.Parse(row["GetDateTime"].ToString());
				}
				if(row["TemValue"]!=null && row["TemValue"].ToString()!="")
				{
					model.TemValue=decimal.Parse(row["TemValue"].ToString());
				}
				if(row["AreaId"]!=null && row["AreaId"].ToString()!="")
				{
					model.AreaId=int.Parse(row["AreaId"].ToString());
				}
				if(row["Id"]!=null && row["Id"].ToString()!="")
				{
					model.Id=int.Parse(row["Id"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select HumValue,GetDateTime,TemValue,AreaId,Id ");
			strSql.Append(" FROM TandHTable ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQLite.QueryDataSet(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM TandHTable ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            object obj = DbHelperSQLite.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.Id desc");
			}
			strSql.Append(")AS Row, T.*  from TandHTable T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQLite.QueryDataSet(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@tblName", DbType.VarChar, 255),
					new SQLiteParameter("@fldName", DbType.VarChar, 255),
					new SQLiteParameter("@PageSize", DbType.Int32),
					new SQLiteParameter("@PageIndex", DbType.Int32),
					new SQLiteParameter("@IsReCount", DbType.bit),
					new SQLiteParameter("@OrderType", DbType.bit),
					new SQLiteParameter("@strWhere", DbType.VarChar,1000),
					};
			parameters[0].Value = "TandHTable";
			parameters[1].Value = "Id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQLite.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod


		#endregion  ExtensionMethod
	}
}

